<?php


namespace app\admin\controller\Admin;


use app\admin\controller\BaseController;
use app\admin\model\Admin\MessageModel;
use app\Request;
use app\Utils\Util;
use Exception;
use think\facade\Db;

class Message extends BaseController
{

    /**
     * 分页查询
     * @param Request $request
     * @return array
     */
    public function getMessageList(Request $request)
    {
        $where = [];
        $param = $request->param();
        $page = !empty($param['page']) ? $param['page'] : 1;
        $limit = !empty($param['limit']) ? $param['limit'] : 10;

        $list = MessageModel::getMessageListWhere($page,$limit,$where);
        $count = MessageModel::getMessageCountWhere($where);
        foreach ($list as &$item){
            $item['details'] = htmlspecialchars_decode($item['details']);
        }
        unset($item);
        return Util::returnPageJson(self::CODE_DATA,$count,$list,"");
    }

    /**
     * 添加
     * @param Request $request
     * @return array
     */
    public function addMessage(Request $request)
    {
        $param = $request->param();
        $title = !empty($param['title']) ? $param['title'] : "";
        $author = !empty($param['author']) ? $param['author'] : "";
        $details = !empty($param['details']) ? htmlspecialchars($param['details']) : "";
        $is_top = !empty($param['is_top']) ? $param['is_top'] : "";

        $data = [
            'title'    => $title,
            'author'   => $author,
            'details'  => $details,
            'is_top'   => $is_top,
            'up_time'  => date("Y-m-d H:i:s",time()),
            'add_time' => date("Y-m-d H:i:s",time())
        ];

        if(!empty($is_top) && $is_top == "on"){
            $data['is_top'] = 1;
        }else{
            $data['is_top'] = 0;
        }

        $id = MessageModel::addMessage($data);
        if($id){
            return Util::returnJson(self::CODE_SUCCESS,$id,"添加成功");
        }else{
            return Util::returnJson(self::CODE_ERROR,null,"添加失败");
        }
    }

    /**
     * 更新
     * @param Request $request
     * @return array
     */
    public function editMessage(Request $request)
    {
        $param = $request->param();
        $id = !empty($param['id']) ? $param['id'] : 0;
        $title = !empty($param['title']) ? $param['title'] : "";
        $author = !empty($param['author']) ? $param['author'] : "";
        $details = !empty($param['details']) ? htmlspecialchars($param['details']) : "";
        $is_top = !empty($param['is_top']) ? $param['is_top'] : "";

        $save = [
            'title'    => $title,
            'author'   => $author,
            'details'  => $details,
            'up_time'  => date("Y-m-d H:i:s",time()),
            'add_time' => date("Y-m-d H:i:s",time())
        ];

        if(!empty($is_top) && $is_top == "on"){
            $save['is_top'] = 1;
        }else{
            $save['is_top'] = 0;
        }

        $flag = MessageModel::updateMessageWhere([['id','=',$id]],$save);
        if($flag){
            return Util::returnJson(self::CODE_SUCCESS,null,"修改成功");
        }else{
            return Util::returnJson(self::CODE_ERROR,null,"修改失败");
        }
    }

    /**
     * 删除
     * @param Request $request
     * @return array
     */
    public function delMessage(Request $request)
    {
        $param = $request->param();
        $id = $param['id'];
        //开启事务
        Db::startTrans();
        try{
            //中间逻辑代码
            $flag = MessageModel::delMessageWhere([['id','=',$id]]);
            if($flag){
                //提交事务
                Db::commit();
                return Util::returnJson(self::CODE_SUCCESS,null,"删除成功");
            }else{
                Db::rollBack();
                return Util::returnJson(self::CODE_ERROR,null,"删除失败4");
            }
        }catch (Exception $e) {
            Db::rollBack();
            return Util::returnJson(self::CODE_ERROR,$e,"删除失败1");
        }
    }

}